package org.bluedream.comm.utils;

import com.alibaba.fastjson.JSON;
import com.google.zxing.WriterException;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.utils.QRC.QRCodeGenerator;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FileLoad
 * @Description TODO
 * @Author foxsand
 * @Data 2020/5/19 9:17
 * @Version
 */
public class FileLoad {

    public void readFileByBytes(String fileName) throws IOException, WriterException {
        File file = new File(fileName);
        InputStream in = null;
        List<Map<Integer, byte[]>> obj = new ArrayList<>();
        try {
            byte[] tmpBytes = new byte[1024];
            int readLength = 0;
            in = new FileInputStream(file);
//            OutputStream os = new FileOutputStream(new File("d:\\demo.mp3"));
            while ((readLength = in.read(tmpBytes)) != -1){
                Map<Integer, byte[]> fileMap = new HashedMap();
                fileMap.put(readLength , tmpBytes);
                obj.add(fileMap);
//                os.write(tmpBytes , 0 , readLength);
//                System.out.write(tmpBytes , 0 , readLength);
            }
//            os.flush();
//            os.close();
            in.close();
        }catch (IOException e1){
            e1.printStackTrace();
        }
        String str = JSON.toJSONString(obj);
        str = EncryptUtil.encodeBase64String(str);
        QRCodeGenerator.generateQRCodeImage(str);
    }

    private void showAvailableBytes(InputStream in){
        try {
            System.out.println(in.available());
        }catch (IOException e1){
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, WriterException {
        String filePath = "d:\\平凡之路.mp3";
        FileLoad f1 = new FileLoad();
        f1.readFileByBytes(filePath);
    }
}
